print("202501141700 - 打印 GardenGuideModules.DataModules.PetData 全量数据")

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function log(s)
	local line = string.format("[%s] %s", os.date("%Y-%m-%d %H:%M:%S"), tostring(s))
	print("[GuidePetData] " .. tostring(s))
	local ok = pcall(function()
		appendfile("guide_petdata_log.txt", line .. "\n")
	end)
	if not ok then
		pcall(function()
			writefile("guide_petdata_log.txt", line .. "\n")
		end)
	end
end

local function safeToString(v)
	local ok, res = pcall(function() return tostring(v) end)
	return ok and res or "[unstringifiable]"
end

local function deepCopySample(tbl, depth, maxDepth, maxItems)
	depth = depth or 0
	maxDepth = maxDepth or 6
	maxItems = maxItems or 2000
	if typeof(tbl) ~= "table" then return tbl end
	if depth >= maxDepth then return "[maxDepth]" end
	local out = {}
	local count = 0
	for k, v in pairs(tbl) do
		count = count + 1
		if count > maxItems then
			out["__truncated__"] = true
			break
		end
		if typeof(v) == "table" then
			out[safeToString(k)] = deepCopySample(v, depth + 1, maxDepth, maxItems)
		else
			out[safeToString(k)] = v
		end
	end
	return out
end

local function requirePetDataModule()
	local path = {
		"Modules", "GardenGuideModules", "DataModules", "PetData"
	}
	local node = ReplicatedStorage
	for _, name in ipairs(path) do
		node = node:FindFirstChild(name)
		if not node then
			return false, "未找到: ReplicatedStorage." .. table.concat(path, ".")
		end
	end
	if not node:IsA("ModuleScript") then
		return false, "目标不是ModuleScript: " .. node:GetFullName()
	end
	local ok, mod = pcall(function() return require(node) end)
	if not ok then
		return false, "require失败: " .. safeToString(mod)
	end
	return true, mod
end

local function exportAll()
	log("🚀 开始打印 ReplicatedStorage.Modules.GardenGuideModules.DataModules.PetData ...")
	local ok, mod = requirePetDataModule()
	if not ok then
		log("❌ " .. tostring(mod))
		return false
	end
	log("✅ require 成功，开始导出...")

    local snapshot
    if typeof(mod) == "table" then
        snapshot = deepCopySample(mod, 0, 10, 10000)
    else
        snapshot = { __warn = "module returned non-table", kind = typeof(mod), value = safeToString(mod) }
    end

	-- JSON 导出
	local okJson, encoded = pcall(function()
		return HttpService:JSONEncode(snapshot)
	end)
	if okJson then
		pcall(function()
			writefile("guide_petdata.json", encoded)
			log("✅ 已导出 guide_petdata.json")
		end)
	else
		log("⚠️ JSON 编码失败")
	end

	-- 文本导出（列出顶层键）
	local lines = {}
	table.insert(lines, "=== GardenGuideModules.DataModules.PetData 顶层键 ===")
	local topCount = 0
	if typeof(mod) == "table" then
		for k, v in pairs(mod) do
			topCount = topCount + 1
			local typeStr = typeof(v)
			local head = string.format("%d. %s (%s)", topCount, safeToString(k), typeStr)
			table.insert(lines, head)
			if typeStr == "table" then
				local sub = 0
				for sk in pairs(v) do
					sub = sub + 1
					if sub <= 10 then
						table.insert(lines, "   - " .. safeToString(sk))
					end
				end
				if sub > 10 then
					table.insert(lines, string.format("   ...还有 %d 个键", sub - 10))
				end
			end
		end
	end
	pcall(function()
		writefile("guide_petdata_keys.txt", table.concat(lines, "\n"))
		log("✅ 已导出 guide_petdata_keys.txt")
	end)

	log("📊 完成，顶层键: " .. tostring(topCount))
	return true
end

exportAll()
